/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.Brand;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.sys.entity.UserDataScope;
import com.jeesite.modules.sys.utils.DictUtils;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/brand")
public class BrandController extends BaseController {

	@Autowired
	private BrandService brandService;

	@Autowired
	private MyProps myProps;
	@Autowired
	private GoodsTypeService goodsTypeService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Brand get(String id, boolean isNewRecord) {
		Brand brand = new Brand();
		if (StringUtils.isNotBlank(id)) {
			brand.setId(id);
			brand = brandService.get(brand);
		}
		if(brand==null){
			brand = new Brand();
		}
		return brand;
	}
	
	/**
	 * 查询列表
	 */

	@RequestMapping(value ="getType")
	@ResponseBody
	public Brand getType(String brandid, Model model) {
		Brand brand =new Brand();
		brand.setId(brandid);
		brand=brandService.get(brand);
	/*	
		GoodsType goodsType = new GoodsType();
		goodsType.setGoodsCode(brand.getType());
		
		goodsTypeService.addDataScopeFilter(goodsType, UserDataScope.CTRL_PERMI_MANAGE);
		
		GoodsType typeL = goodsTypeService.get(goodsType);*/
		
	//	String type=brand.getType();
		
		/*if(StringUtils.isNotBlank(type)){
			String[] typeArr=type.split(",");
			String typeName="";
			for(String s:typeArr){
				 typeName+=DictUtils.getDictLabel("zt_goodsType_type", s, "0")+",";
			}
			typeName=typeName.substring(0,(typeName.length()-1));
			typeL.setTypeName(typeName);
		}*/
		
		return brand;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("brand:brandData:view")
	@RequestMapping(value = { "list", "" })
	public String list(Brand brand, Model model) {
		model.addAttribute("brand", brand);
		
		GoodsType goodsType = new GoodsType();
		goodsType.setTreeLevel(0);
		goodsTypeService.addDataScopeFilter(goodsType, UserDataScope.CTRL_PERMI_MANAGE);
		
		List<GoodsType> typeList = goodsTypeService.findList(goodsType);
		model.addAttribute("typeList", typeList);
		return "modules/ztGoods/brandList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("brand:brandData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Brand> listData(Brand brand,
			HttpServletRequest request, HttpServletResponse response) {
		Page<Brand> page = brandService.findPage(
				new Page<Brand>(request, response), brand);
		for(Brand b:page.getList()){
			if(StringUtils.isNotBlank(b.getType())){
				GoodsType gt=goodsTypeService.get(b.getType());
				if(gt!=null){
					b.setTypeName(gt.getName());
				}
			}
				
		}

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("brand:brandData:view")
	@RequestMapping(value = "form")
	public String form(Brand brand, Model model) {
		
		model.addAttribute("brand", brand);
		return "modules/ztGoods/brandForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("brand:brandData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Brand brand) {
		brandService.save(brand);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}
	/**
	 * 不显示
	 */
	@RequiresPermissions("brand:brandData:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Brand brand) {
		brand.setIsshow("0");
		brandService.save(brand);
		return renderResult(Global.TRUE, text("更改不显示成功"));
	}
	
	/**
	 * 显示
	 */
	@RequiresPermissions("brand:brandData:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Brand brand) {
		brand.setIsshow("1");
		brandService.save(brand);
		return renderResult(Global.TRUE, text("更改显示成功"));
	}
	/**
	 * 删除数据
	 */
	@RequiresPermissions("brand:brandData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Brand brand) {
		brandService.delete(brand);
		return renderResult(Global.TRUE, text("删除数据成功！"));
	}

}